package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.exception.CustomException;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private DishMapper dishMapper;

    @Override
    @Transactional
    public void saveWithFlavor(DishDto dto) {
        //保存Dish的基本信息
        this.save(dto);
        //口味信息保存到dis_flavor
        List<DishFlavor> flavors = dto.getFlavors();
        //设置dishId
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dto.getId());
        }
        dishFlavorService.saveBatch(flavors);
    }

    @Override
    public Page<DishDto> pageByName(int page, int pageSize, String name) {
        Page<DishDto> pageInfo = new Page<>(page, pageSize);
        return dishMapper.pageByName(pageInfo, name);
    }

    @Override
    public DishDto getByIdWithFlavor(Long id) {
        Dish dish = getById(id);
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish,dishDto);
        //查询当前菜品对应的口味信息，从dish_flavor表查询
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dish.getId());
        List<DishFlavor> flavors = dishFlavorService.list(queryWrapper);
        dishDto.setFlavors(flavors);
        return dishDto;
    }

    @Override
    @Transactional
    public void updateWithFlavor(DishDto dto) {
        //1. 根据id修改菜品基本信息
        updateById(dto);
        Long dishId = dto.getId(); //获取用户传递过来的菜品id
        //2. 先删除这个菜品的所有口味 delete from dish_flavor where dish_id=1655772643288092674;
        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
        lqw.eq(DishFlavor::getDishId, dishId);
        dishFlavorService.remove(lqw);
        //3. 添加新的口味
        List<DishFlavor> flavors = dto.getFlavors();
        //设置dishId
        flavors.forEach(dishFlavor -> dishFlavor.setDishId(dishId));
        dishFlavorService.saveBatch(flavors);
    }

    @Override
    public void deleteById(List<Long> ids) {
        LambdaQueryWrapper<Dish> dLqw = new LambdaQueryWrapper<>();
        //select count(*) from dish where status=1 and id in (?);
        dLqw.in(Dish::getId, ids);
        dLqw.eq(Dish::getStatus, 1);
        int count = count(dLqw);
        if (count!=0){
            throw new CustomException("菜品起售中,不能删除");
        }
        //如若停售状态，删除菜品表中的数据---setmeal
        removeByIds(ids);
        //delete from setmeal_dish where setmeal_id in (1,2,3)

    }

    @Override
    public void updateByStatus( Integer status,List<Long> ids) {
        //update dish set status = 1 where id=?;
        for (Long id : ids) {
            dishMapper.updateByStatus(status,id);
        }
    }

 /*   LambdaQueryWrapper<Dish> dLqw = new LambdaQueryWrapper<>();
    //select count(*) from dish where status=1 and id in (?);
        dLqw.in(Dish::getId, ids);
        dLqw.eq(Dish::getStatus, 1);*/
}